package controller

import (
	"strconv"

	"system-admin/internal/app/web/service/merchant"
	"system-admin/internal/app/web/service/user"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
)

type Merchant struct{}

// AllMerchant 获取所有商户
func (Merchant) All(c *gin.Context) {
	all, total := merchant.AllMerchant(c.GetString("user_id"), c.GetString("merchant_id"), c.Query("fields"), 0, 0)
	if total <= 0 {
		response.JSON(c, gin.H{
			"list":  []string{},
			"total": 0,
		}, nil)
	} else {
		response.JSON(c, gin.H{
			"list":  all,
			"total": total,
		}, nil)
	}
}

// List 商户列表
func (Merchant) List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	list, total := merchant.MerchantList(c.GetString("user_id"), c.Query("name"), page, limit)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, nil)
}

// Add 新增商户
func (Merchant) Add(c *gin.Context) {
	var params struct {
		Name         string `json:"name"`
		ContactUser  string `json:"contact_user"`
		ContactPhone string `json:"contact_phone"`
		ContactAddr  string `json:"contact_addr"`
	}
	c.ShouldBindJSON(&params)
	merchantID, err := merchant.AddMerchant(c.GetString("user_id"), params.Name, params.ContactUser, params.ContactPhone, params.ContactAddr)
	if err != nil {
		response.JSON(c, gin.H{
			"merchant_id": "",
		}, err)
	} else {
		response.JSON(c, gin.H{
			"merchant_id": merchantID,
		}, err)
	}
}

// Edit 编辑商户
func (Merchant) Edit(c *gin.Context) {
	var params struct {
		Name         string `json:"name"`
		MerchantID   string `json:"merchant_id"`
		ContactUser  string `json:"contact_user"`
		ContactPhone string `json:"contact_phone"`
		ContactAddr  string `json:"contact_addr"`
	}
	c.ShouldBindJSON(&params)
	err := merchant.EditMerchant(c.GetString("user_id"), params.MerchantID, params.Name, params.ContactUser, params.ContactPhone, params.ContactAddr)
	response.JSON(c, nil, err)
}

// Delete 删除商户
func (Merchant) Delete(c *gin.Context) {
	var params struct {
		MerchantID string `json:"merchant_id"`
	}
	c.ShouldBindJSON(&params)
	err := merchant.DeleteMerchant(c.GetString("user_id"), params.MerchantID)
	response.JSON(c, nil, err)
}

// SetAdminUser 设置商户管理员
func (Merchant) SetAdminUser(c *gin.Context) {
	var params struct {
		MerchantID string `json:"merchant_id"`
		RoleID     string `json:"role_id"`
		Username   string `json:"username"`
		Password   string `json:"password"`
	}
	c.ShouldBindJSON(&params)
	err := user.SetMerchantAdminUser(c.GetString("user_id"), params.MerchantID, params.RoleID, params.Username, params.Password)
	response.JSON(c, nil, err)
}

// CreateAPIToken 创建apiToken
func (Merchant) CreateAPIToken(c *gin.Context) {
	var params struct {
		MerchantID string `json:"merchant_id"`
	}
	c.ShouldBindJSON(&params)
	token, err := merchant.CreateAPIToken(c.GetString("merchant_id"), params.MerchantID)
	if err != nil {
		response.JSON(c, map[string]interface{}{
			"token": "",
		}, err)
		return
	}
	response.JSON(c, map[string]interface{}{
		"token": token,
	}, nil)
}
